package fr.vvins.core.services;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import javax.annotation.Resource;
import javax.ws.rs.Path;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Component;

import fr.vvins.core.biz.catalog.CatalogException;
import fr.vvins.core.biz.catalog.CatalogManager;
import fr.vvins.core.biz.catalog.dto.VigneronDto;
import fr.vvins.core.biz.catalog.dto.VigneronDtoInfo;
import fr.vvins.core.biz.catalog.dto.VinDto;
import fr.vvins.core.biz.catalog.dto.VinDtoInfo;
import fr.vvins.core.biz.exception.BizException;
import fr.vvins.core.common.TechnicalException;
import fr.vvins.core.common.dto.BeanDtoInfo;

@Path("/catalog")
@Component("catalogService")
public class CatalogServiceImpl implements CatalogService {
	
	@Resource(name="catalogManager")
	private CatalogManager catalogManager;


	@Override
	public List<VigneronDtoInfo> getAllVigneron() throws BizException, TechnicalException  {
		return getCatalogManager().getVignerons();
	}
	
	@Override
	public VigneronDto getVigneron(String vigneronId) throws BizException, TechnicalException {
		if(StringUtils.isEmpty(vigneronId)){
			throw new CatalogException(CatalogException.EMPTY_PARAM, "Vigneron id must not be empty");
		}
		if(vigneronId.startsWith("core:")){
			vigneronId = vigneronId.replace("core:", "");
			return getCatalogManager().getVigneron(Integer.parseInt(vigneronId));
		} else if (vigneronId.startsWith("content:")) {
			vigneronId = vigneronId.replace("content:", "");
			return getCatalogManager().getVigneronByContent(Integer.parseInt(vigneronId));
		} else if (vigneronId.startsWith("key:")) {
			vigneronId = vigneronId.replace("key:", "");
			return getCatalogManager().getVigneronByKey(vigneronId);
		}
		return getCatalogManager().getVigneron(Integer.parseInt(vigneronId));
	}
	
	@Override
	public Set<VinDtoInfo> getVinByVigneron(String vigneronId) throws BizException, TechnicalException {
		if(StringUtils.isEmpty(vigneronId)){
			throw new CatalogException(CatalogException.EMPTY_PARAM, "Vigneron id must not be empty");
		}
		if(vigneronId.startsWith("core:")){
			vigneronId = vigneronId.replace("core:", "");
			return getCatalogManager().getVinsVigneron(Integer.parseInt(vigneronId));
		} else if (vigneronId.startsWith("content:")) {
			vigneronId = vigneronId.replace("content:", "");
			return getCatalogManager().getVinsVigneronByContent(Integer.parseInt(vigneronId));
		} else if (vigneronId.startsWith("key:")) {
			vigneronId = vigneronId.replace("key:", "");
			return getCatalogManager().getVinsVigneronByKey(vigneronId);
		} 
		return getCatalogManager().getVinsVigneron(Integer.parseInt(vigneronId));
	}	
	
	@Override
	public VinDto getVin(String vinId) throws BizException, TechnicalException  {
		if(StringUtils.isEmpty(vinId)){
			throw new CatalogException(CatalogException.EMPTY_PARAM, "Vin id must not be empty");
		}
		if(vinId.startsWith("core:")){
			vinId = vinId.replace("core:", "");
			return getCatalogManager().getVin(Integer.parseInt(vinId));
		} else if (vinId.startsWith("content:")) {
			vinId = vinId.replace("content:", "");
			return getCatalogManager().getVinByContent(Integer.parseInt(vinId));
		} else if (vinId.startsWith("key:")) {
			vinId = vinId.replace("key:", "");
			return getCatalogManager().getVinByKey(vinId);
		}
		return getCatalogManager().getVin(Integer.parseInt(vinId));
	}
	
	@Override
	public List<BeanDtoInfo> getList(String listId) throws BizException, TechnicalException {
		if(StringUtils.isEmpty(listId)){
			throw new CatalogException(CatalogException.EMPTY_PARAM, "List id must not be empty");
		}
		if(listId.startsWith("core:")){
			listId = listId.replace("core:", "");
			return getCatalogManager().getListByKey(listId);
		} else if (listId.startsWith("key:")){
			listId = listId.replace("key:", "");
			return getCatalogManager().getListByKey(listId);
		}
		return getCatalogManager().getList(Integer.parseInt(listId));
	}
	
	@Override
	public BeanDtoInfo getContent(String contentId) throws BizException, TechnicalException {
		if(contentId.startsWith("core:")){
			contentId = contentId.replace("core:", "");
			return getCatalogManager().getContent(Integer.parseInt(contentId));
		} else if (contentId.startsWith("content:")) {
			contentId = contentId.replace("content:", "");
			return getCatalogManager().getContent(Integer.parseInt(contentId));
		} else if (contentId.startsWith("key:")) {
			contentId = contentId.replace("key:", "");
			return getCatalogManager().getContentByKey(contentId);
		}
		return getCatalogManager().getVin(Integer.parseInt(contentId));
	
	}
	
	@Override
	/**
	* J'y comprends rien
	*TODO : related content implementation
	**/
	public List<BeanDtoInfo> getRelatedVigneron(String vigneronId) throws BizException, TechnicalException {
		List<BeanDtoInfo> r = new ArrayList<BeanDtoInfo>();
		for(VigneronDtoInfo d : getAllVigneron()){
			r.add(d);
		}
		return r;
	}

	public CatalogManager getCatalogManager() {
		return catalogManager;
	}

	public void setCatalogManager(CatalogManager catalogManager) {
		this.catalogManager = catalogManager;
	}

	

}
